package com.pacs.config;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.druid.pool.DruidDataSource;
//import com.ayez.control.DispatchControl;
//import com.ayez.control.TimerController;
import com.pacs.control.WeixinJSControl;
import com.pacs.control.WeixinMenuControl;
import com.pacs.control.WeixinMsgController;
import com.pacs.control.WeixinQRCodeController;
import com.pacs.control.WeixinWebController;
import com.pacs.interceptor.AccessControlInterceptor;
import com.pacs.model._MappingKit;
import com.pacs.service.ServiceFactory;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.jfinal.weixin.sdk.api.ApiConfigKit;

public class PacsConfig extends JFinalConfig {
	
	// 本地开发模式
    private boolean isLocalDev = false;
    
    /**
     * 如果生产环境配置文件存在，则优先加载该配置，否则加载开发环境配置文件
     * @param pro 生产环境配置文件 
     * @param dev 开发环境配置文件
     */
    public void loadProp(String pro, String dev) {
        try {
            PropKit.use(pro);
        }
        catch (Exception e) {
            PropKit.use(dev);
            isLocalDev = true;
        }
    }

    @Override
	public void afterJFinalStart() {
		super.afterJFinalStart();
//		DispatchControl.init();
		ServiceFactory.init();
	}
    
	@Override
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		loadProp("config.txt", null);
		me.setDevMode(PropKit.getBoolean("devMode", false));
		me.setEncoding("utf-8");
		me.setViewType(ViewType.JSP);
		// 设置上传文件保存的路径
		me.setBaseUploadPath(PathKit.getWebRootPath() + File.separator + "myupload");
		// ApiConfigKit 设为开发模式可以在开发阶段输出请求交互的 xml 与 json 数据
		ApiConfigKit.setDevMode(me.getDevMode());
	}

	@Override
	public void configEngine(Engine me) {
	}

	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub

	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		me.add(new AccessControlInterceptor());

	}

	@Override
	public void configPlugin(Plugins me) {
		// 配置 Druid 数据库连接池插件
		DruidPlugin druidPlugin = createDBPlugin();
		druidPlugin.start();
		DruidDataSource dataSource = (DruidDataSource)druidPlugin.getDataSource();
		  List<String> list = new ArrayList<>();
		  list.add("set names utf8mb4;");
		  dataSource.setConnectionInitSqls(list);
		me.add(druidPlugin);

		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		me.add(arp);

		// Redis插件
		RedisPlugin redisPlugin = createRedisPlugin();
		redisPlugin.setSerializer(new JsonSerializer());
		me.add(redisPlugin);

		// 所有配置在 MappingKit 中搞定
		_MappingKit.mapping(arp);
	}
	
	private DruidPlugin createDBPlugin(){
		DruidPlugin plugin = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
		return plugin;
	}
	
	private RedisPlugin createRedisPlugin(){
		String pwd = PropKit.get("redis-password");
		RedisPlugin plugin = null;
		if (pwd == null){
			plugin = new RedisPlugin("Aibei", PropKit.get("redis-host"), PropKit.getInt("redis-port"), 10000);
		}else{
			plugin = new RedisPlugin("Aibei", PropKit.get("redis-host"), PropKit.getInt("redis-port"), 10000, pwd);
		}
		return plugin;
	}
	
	@Override
	public void configRoute(Routes me) {
//		me.add("/dispatch",DispatchControl.class);
		me.add("/msg", WeixinMsgController.class);
		me.add("/wx", WeixinWebController.class);
		me.add("/wxjs", WeixinJSControl.class);
		me.add("/wxqr", WeixinQRCodeController.class);
		me.add("/wxmenu", WeixinMenuControl.class);


	}

}
